$(function() {
    init_vue();
});

function init_vue() {
    var vue = new Vue({
        el : "#myapp",
        data : {
            level:level,
            startRow : 0,
            endRow : 0,
            total : 0,
            firstPage : 0,
            lastPage : 0,
            pageNum : 1,
            pageSize : 15,
            list : null,
            setting : null,
            navigatepageNums : null,
            sum_balance:0
        },
        events : {},
        methods : {
            init : function() {
                close_layuiLayerLoad();
                this.getData();
            },
            showPage : function(page) {
                // alert(page);
                this.pageNum = page;
                this.getData();
            },
            getData : function() {
                showLoad();
                var t = this;
                postMsg(root+"/agent/tolist", {
                    page_num : t.pageNum,
                    page_size : t.pageSize,
                    level:t.level
                }, function(i) {
                    t.list = null;
                    t.list = i.data.list;
                    t.setting = i.data.setting;
                    t.startRow = i.data.startRow;
                    t.endRow = i.data.endRow;
                    t.total = i.data.total;
                    t.firstPage = i.data.firstPage;
                    t.lastPage = i.data.lastPage;
                    t.navigatepageNums = i.data.navigatepageNums;
                    t.sum_balance = i.sum_balance;
                    hideLoad();
                });
            },
            changeState : function(id, state) {
                var t = this;
                showLoad();
                postMsg(root+"/agent/changeState", {id:id, state:state}, function(i) {
                    if (i == 1) {
                        for (var i=0; i<t.list.length; i++) {
                            if (t.list[i].id == id) {
                                t.list[i].state = state;
                            }
                        }
                        if (state == 2) {
                            $.scojs_message('账号已停用', $.scojs_message.TYPE_ERROR);
                        } else if (state == 1) {
                            $.scojs_message('账号已启用', $.scojs_message.TYPE_OK);
                        }
                    }
                    hideLoad();
                });
            },
            changeAuditState : function (id, audit_state) {
                var t = this;
                showLoad();
                postMsg(root+"/agent/changeAuditState", {id:id, audit_state:audit_state}, function(r) {
                    hideLoad();
                    layer.alert(r.msg, {end:function () {
                        if (r.code == 1) {
                            for (var i=0; i<t.list.length; i++) {
                                if (t.list[i].id == id) {
                                    t.list[i].audit_state = audit_state;
                                }
                            }
                            if (audit_state == 1) {
                                var agent = new Object();
                                agent.id = r.id;
                                agent.name = r.name;
                                t.$broadcast('openAgentCommissionSetWin', {title:'设置返佣', agent:agent});
                            }
                        }
                    }});
                });
            },
            openAgentCommissionSetWin : function (agent) {
                this.$broadcast('openAgentCommissionSetWin', {title:'设置返佣', agent:agent});
            },
            sel_belong : function (agent) {
                this.$broadcast('openTreeOrganizationWin', {agent:agent});
            }
        }
    });
    vue.init();
}